package com.ks.core.framework.cpsupload.demos.orderpayinvoice.orderhandler;

import com.ks.core.framework.cpsupload.demos.orderpayinvoice.event.PaymentCallbackEvent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 观察者模式：支付回调处理器
class PaymentCallbackHandler {
    private final Map<String, Order> orderCache = new ConcurrentHashMap<>();
    
    public void processCallback(PaymentCallbackEvent event) {
        Order order = orderCache.get(event.getOrderId());
        if (order == null) {
            // 从数据库加载订单
            order = loadOrderFromDb(event.getOrderId());
            orderCache.put(event.getOrderId(), order);
        }
        
        if (event.isSuccess()) {
            // 添加支付记录
            order.addPaymentRecord(event.getPaymentType(), event.getAmount(), event.getTransactionId());
        }
    }
    
    private Order loadOrderFromDb(String orderId) {
        // 模拟从数据库加载
        return new ConferenceOrder(); // 简化
    }
}